package qsoft.com.tp51_scanner_module

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent

interface IonReceived {
    fun onReceived(s: String)
}

typealias OnReceived = (String) -> Unit

/**
 * action 扫描广播的action
 * data 扫描广播的条码extra
 */
class TP51_Scanner(var lifecycle: Lifecycle, var action: String? = null, var data: String? = null) : LifecycleObserver {
    private val BARCODEPORT_RECEIVEDDATA_ACTION = "com.android.serial.BARCODEPORT_RECEIVEDDATA_ACTION" //TP51的扫描广播action
    private val BARCODEPORT_RECEIVEDDATA_EXTRA_DATA = "DATA" //TP51的扫描广播的条码extra

    init {
        this.action = this.action ?: BARCODEPORT_RECEIVEDDATA_ACTION
        this.data = this.data ?: BARCODEPORT_RECEIVEDDATA_EXTRA_DATA
        this.lifecycle.addObserver(this)
    }

    private val mResultReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val action = intent.action
            if (action == this@TP51_Scanner.action) {
                if (intent.extras!!.containsKey(data)) {
                    val data = intent.extras!![data]

                    val datastr = if (data!!.javaClass == "".javaClass) data.toString() else String(data as ByteArray)

                    if (mOnReceived != null) {
                        mOnReceived!!.onReceived(datastr)
                    } else if (mOnReceivedKotlin !=null) {
                        mOnReceivedKotlin!!.invoke(datastr)
                    }
                }
            }
        }
    }

    private var mOnReceived: IonReceived? = null //java使用接口方式调用
    private var mOnReceivedKotlin: OnReceived? = null //kotlin使用函数参数方式调用

    private lateinit var mContext: Context

    /**
     * 设置接收到条码后的回调（java使用接口方式调用）
     */
    fun setOnReceived(context: Context, onReceived: IonReceived) {
        if (mOnReceived == null) { //每一次设置回调
            mOnReceived = onReceived
            mContext = context
        } else { //修改回调
            unRegisterReceiver()
            mOnReceived = onReceived
            mContext = context
            registerReceiver()
        }
    }

    /**
     * 设置接收到条码后的回调（kotlin使用函数参数方式调用）
     */
    fun setOnReceivedKotlin(context: Context, onReceived: OnReceived) {
        if (mOnReceivedKotlin == null) { //每一次设置回调
            mOnReceivedKotlin = onReceived
            mContext = context
        } else { //修改回调
            unRegisterReceiver()
            mOnReceivedKotlin = onReceived
            mContext = context
            registerReceiver()
        }
    }

    private fun registerReceiver() {
        val intFilter = IntentFilter(action)
        mContext.registerReceiver(mResultReceiver, intFilter)
    }

    private fun unRegisterReceiver() {
        try {
            mContext.unregisterReceiver(mResultReceiver)
        } catch (e: Exception) {
            e.printStackTrace()
            Log.v("q-soft", e.message!!)
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun resume() {
        registerReceiver()
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun pause() {
        unRegisterReceiver()
    }
}